package org.example.petsystem.controller;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/Register")
@CrossOrigin(origins="*")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    //管理员给用户注册
    @RequestMapping("/RegisterUser")
    public ResultBody RegisterUser(@RequestBody Map<String,Object> map) {
        //检验是否缺少参数

        if (map.get("username") == null || map.get("name") == null || map.get("password") == null ||
                map.get("confirmPassword") == null || map.get("age") == null || map.get("resource") == null ||
                map.get("state") == null ||  map.get("phonenumber") == null || map.get("email") == null || map.get("address") == null) {
            return new ResultBody(404, "缺少参数", false);
        }

        String username = (String) map.get("username");//用户名
        String name = (String) map.get("name");//用户真实姓名
        String password= (String) map.get("password");//用户密码
        String confirmPassword = (String) map.get("confirmPassword");//用户重复密码
        String phonenumber = (String) map.get("phonenumber");//用户联系方式
        String email = (String) map.get("email");//用户邮箱
        String address = (String) map.get("address");//用户地址
        int age = (int)map.get("age");
        String resourceStr = (String) map.get("resource");
        int resource = Integer.parseInt(resourceStr);

        String stateStr = (String) map.get("state");
        int state = Integer.parseInt(stateStr);
        //检验重复密码是否正确
        if(!password.equals(confirmPassword))
        {
            return new ResultBody(400,"重复密码错误",false);
        }
        //检验是否重复注册
        //电话号码重复
        if(registerService.checkPhoneNumber(phonenumber)==1){
            return new ResultBody(400,"电话重复",false);
        }
        //昵称重复
        if(registerService.checkUserName(username)==1) {
            return new ResultBody(400,"昵称重复",false);
        }
        //邮箱重复
        if(registerService.checkEmail(email)==1)
        {
            return new ResultBody(400,"邮箱重复",false);
        }
        //根据类型传数据
        if(registerService.register(username, name, password, age, resource, state, phonenumber, email, address)==1)
        {
            return new ResultBody(200,"注册成功",true);
        }
        else
            return new ResultBody(400,"注册失败",false);

    }
}
